home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / dicecache / dicecache.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  3KB  |  139 lines

  1. /*
  2.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  3.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  4.  *    DICE-LICENSE.TXT.
  5.  */
  6.  
  7. /*
  8.  *  DICECACHE.C
  9.  *
  10.  *  DICECACHE [ON][OFF][MAXFILE][MAXSIZE][ADD][REM]
  11.  *
  12.  */
  13. #ifdef AMIGA
  14. #include <lib/version.h>
  15.  
  16. #ifdef _DCC
  17. IDENT("DiceCache",".57");
  18. DCOPYRIGHT;
  19. #endif
  20. #endif
  21.  
  22. #include <stdio.h>
  23. #include <exec/types.h>
  24. #include <exec/memory.h>
  25. #include <clib/exec_protos.h>
  26. #include <clib/dicecache_protos.h>
  27.  
  28. char Buf[1024];
  29.  
  30. extern void *DiceCacheBase;
  31.  
  32. void FlushMemory(void);
  33.  
  34. main(ac, av)
  35. char *av[];
  36. {
  37.     short i;
  38.     short flushit = 0;
  39.     long la[6];
  40.  
  41.     puts("DICECACHE V1.00");
  42.  
  43.     if (DiceCacheBase == NULL) {
  44.     DiceCacheBase = OpenLibrary("dicecache.library", 0);
  45.     if (DiceCacheBase == NULL) {
  46.         puts("Unable to open dicecache.library");
  47.         exit(1);
  48.     }
  49.     }
  50.     DiceCacheGet(la, 6);
  51.  
  52.     for (i = 1; i < ac; ++i) {
  53.     char *ptr = av[i];
  54.  
  55.     if (stricmp(ptr, "?") == 0) {
  56.         puts("DICECACHE [ON][OFF][FLUSH [n]][MAXFILE n][MAXSIZE n][ADD suffix][REM suffix][TEST]");
  57.     } else if (stricmp(ptr, "ON") == 0) {
  58.         DiceCacheEnable();
  59.     } else if (stricmp(ptr, "OFF") == 0) {
  60.         DiceCacheDisable();
  61.         flushit = 1;
  62.     } else if (stricmp(ptr, "FLUSH") == 0) {
  63.         long bytes;
  64.  
  65.         if (av[i+1] == NULL || (bytes = strtol(av[i+1], NULL, 0)) == 0)
  66.         bytes = 0x1FFFFFFF;
  67.         else
  68.         ++i;
  69.         DiceCacheFlush(bytes);
  70.     } else if (stricmp(ptr, "MAXFILE") == 0) {
  71.         la[3] = strtol(av[++i], NULL, 0);
  72.         DiceCacheSet(la, 4);
  73.     } else if (stricmp(ptr, "MAXSIZE") == 0) {
  74.         la[2] = strtol(av[++i], NULL, 0);
  75.         DiceCacheSet(la, 4);
  76.     } else if (stricmp(ptr, "ADD") == 0) {
  77.         DiceCacheAddSuffix(av[++i]);
  78.     } else if (stricmp(ptr, "REM") == 0) {
  79.         DiceCacheRemSuffix(av[++i]);
  80.     } else if (stricmp(ptr, "TEST") == 0) {
  81.         long size;
  82.         void *dc;
  83.         char *buf;
  84.  
  85.         if (dc = DiceCacheOpen(ptr, "r", &size)) {
  86.         printf("Open %s size %d\n", ptr, size);
  87.         if (buf = DiceCacheSeek(dc, 0, &size)) {
  88.             printf("BUFFER %08lx size %d\n\n", buf, size);
  89.             write(1, buf, (size > 256) ? 256 : size);
  90.             puts("");
  91.         } else {
  92.             printf("Seek failed\n");
  93.         }
  94.         DiceCacheClose(dc);
  95.         } else {
  96.         printf("DiceCacheOpen() failed\n");
  97.         }
  98.     } else {
  99.         printf("Bad option: %s\n", ptr);
  100.     }
  101.     }
  102.  
  103.     DiceCacheGet(la, 6);
  104.     DiceCacheGetSuffixes(Buf, sizeof(Buf));
  105.  
  106.     if (flushit) {
  107.     DiceCacheFlush(0x1FFFFFFF);
  108.     CloseLibrary(DiceCacheBase);
  109.     DiceCacheBase = NULL;
  110.     FlushMemory();
  111.     } else {
  112.     printf("Cached %d/%d/%d filemax=%d hit=%d/%d (%2d%%) suffixes=%s\n",
  113.         la[0],
  114.         la[1],
  115.         la[2],
  116.         la[3],
  117.         la[4],
  118.         la[5],
  119.         la[4] * 100 / la[5],
  120.         Buf
  121.     );
  122.     }
  123.     return(0);
  124. }
  125.  
  126. void
  127. FlushMemory()
  128. {
  129.     void *ptr;
  130.  
  131.     Forbid();
  132.     if (ptr = AllocMem(0x03FFFFFF, MEMF_PUBLIC)) {
  133.     FlushMemory();
  134.     FreeMem(ptr, 0x03FFFFFF);
  135.     }
  136.     Permit();
  137. }
  138.  
  139.